<template>
  <div>
    <div class="demo-input-suffix gl_searchBox">
      <el-row :gutter="20">
        <el-col :span="4">
          <p>地区</p>
          <el-select v-model="provinceCode" placeholder="请选择省份" size="mini" filterable @change="changeProvince">
            <el-option v-for="item in provinceList" :key="item.code" :label="item.name" :value="item.code" />
          </el-select>
        </el-col>
        <el-col :span="4">
          <el-select v-model="cityCode" placeholder="请选择城市" class="search_btn" size="mini" filterable @change="changeCity">
            <el-option v-for="item in cityList" :key="item.code" :label="item.name" :value="item.code" />
          </el-select>
        </el-col>
        <el-col :span="4">
          <el-select v-model="countryCode" placeholder="请选择区域" class="search_btn" size="mini" filterable @change="changeCounty">
            <el-option v-for="item in countryList" :key="item.code" :label="item.name" :value="item.code" />
          </el-select>
        </el-col>
        <el-col :span="4">
          <p>性别</p>
          <el-select v-model="GenderCode" size="mini" placeholder="请选择性别" @change="getPageList()">
            <el-option label="全部" :value="null" />
            <el-option label="男" value="M" />
            <el-option label="女" value="W" />
          </el-select>
        </el-col>
        <el-col :span="4">
          <el-input v-model="selectedMsg.Keyword" placeholder="请输入姓名" class="search_btn" size="mini" clearable @change="getPageList()" />
        </el-col>
        <el-col :span="4" align="right" class="search_btn">
          <el-button type="primary" size="mini" @click="getPageList()">{{ $t('button.query') }}</el-button>
          <el-button type="warning" size="mini" @click="resetData">{{ $t('button.Reset') }}</el-button>
        </el-col>
      </el-row>
    </div>
    <el-row>
      <el-button style="margin-top:10px" type="primary" :loading="buLoading" size="mini" @click="reportData">导出积分表</el-button>
    </el-row>
    <el-table v-loading="loading" size="mini" :data="tableData" class="gl_table_noBorder gl_mt_10 gl_mb_15" style="width: 100%">
      <el-table-column align="center" :width="40">
        <template slot-scope="{ row }">
          <div v-if="row.finalRank === 1" class="allNumber numberOne" />
          <div v-else-if="row.finalRank === 2" class="allNumber numberTwo" />
          <div v-else-if="row.finalRank === 3" class="allNumber numberThree" />
          <div v-else class="allNumber" />
        </template>
      </el-table-column>
      <el-table-column label="排名" prop="finalRank" :width="70" />
      <el-table-column label="姓名" :min-width="150">
        <template slot-scope="{ row }">
          <div class="image_name">
            <img class="table_photo" :src="getStaticFileUrl(row.photoUrl)" alt="">
            <div class="table_name ">{{ row.name }}</div>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="gender" label="性别" />
      <el-table-column prop="organization" label="所属机构" min-width="160">
        <template slot-scope="{ row }">
          <el-popover placement="bottom" width="300" trigger="hover" :content="row.organization">
            <span slot="reference" class="table_name">{{ row.organization ? row.organization.length > 10 ? row.organization.substr(0, 10) + '...' : row.organization : '' }}</span>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="comment" label="所属地区" min-width="160">
        <template slot-scope="{ row }">
          <el-popover placement="bottom" width="300" trigger="hover" :content="row.comment">
            <span slot="reference" class="table_name">{{ row.comment ? row.comment.length > 10 ? row.comment.substr(0, 10) + '...' : row.comment : '' }}</span>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="goldCount" label="金牌" />
      <el-table-column prop="silverCount" label="银牌" />
      <el-table-column prop="bronzeCount" label="铜牌" />
      <el-table-column prop="winPercent" label="带队胜率">
        <template slot-scope="{ row }">
          {{ row.winPercent !== null && row.winPercent !== undefined ? (row.winPercent) % 1 === 0 ? (row.winPercent * 100) + '%' : (row.winPercent * 100).toFixed(2) + '%' : '--' }}
        </template>
      </el-table-column>
      <el-table-column prop="realPoints" label="成绩总分" />
      <el-table-column prop="finalPoints" label="实际展示总分" :min-width="100" />
      <el-table-column width="100" label="操作">
        <template slot-scope="{ row }">
          <el-button size="mini" type="text" @click="clickEidt(row)">积分修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <edit-integral ref="editIntegral" />
    <!-- 分页 -->
    <el-row style="text-align: right;">
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="selectedMsg.MaxResultCount"
        :current-page="selectedMsg.Page"
        @size-change="changePageSize"
        @current-change="changePage"
      />
    </el-row>
  </div>
</template>

<script>
import { api_GET_DownLoadFileAddHeader } from '@/extend/api/file';
import { mapState, mapActions } from 'vuex';
import editIntegral from '../editIntegral.vue';
import { errorText } from '@/lib';
import { api_GET_EventsManagementEventResultPlatformPointRankList } from '@/extend/api/gameswaggerApi/app';
import { Api_GET_EventsManagementEventResultPlatformPointRankListQO } from '@/lib/form/game/swaggerForm';
import {
  api_GET_Provinces,
  api_GET_CitiesProvinceCode,
  api_GET_CountiesCityCode
} from '@/extend/api/gameswaggerApi/district.js';
export default {
  components: { editIntegral },
  data () {
    return {
      selectedMsg: new Api_GET_EventsManagementEventResultPlatformPointRankListQO(),
      GenderCode: null,
      provinceCode: null,
      cityCode: null,
      countryCode: null,
      provinceList: [],
      cityList: [],
      countryList: [],
      tableData: [],
      loading: false,
      total: 0,
      buLoading: false
    };
  },
  computed: {
    ...mapState({
      dict: state => state.dict.dict,
      cache: state => state.dict.cache
    })
  },
  created() {
    this.apiGetProvinces();
    this.getPageList();
  },
  methods: {
    // 若没有字典信息，外部可直接调用方法获取字典数据
    ...mapActions({ getDict: 'dict/getDict', getDictByType: 'dict/getDictByType' }),
    // 获取 Provinces 字典
    apiGetProvinces () {
      api_GET_Provinces()
        .then(
          res => {
            res.unshift({ code: null, name: '全部', type: null });
            this.provinceList = res;
          }
        )
        .catch(
          error => {
            const _msg = error.error.message || error.message;
            this.$message({ type: 'error', message: _msg });
          }
        );
    },
    // 获取 Cities 字典
    apiGetCitiesByProvince () {
      api_GET_CitiesProvinceCode(this.provinceCode)
        .then(
          res => {
            res.unshift({ code: null, name: '全部', type: null });
            this.cityList = res;
          }
        )
        .catch(
          error => {
            const _msg = error.error.message || error.message;
            this.$message({ type: 'error', message: _msg });
          }
        );
    },
    // 获取 Counties 字典
    apiGetCountiesByCity () {
      api_GET_CountiesCityCode(this.cityCode)
        .then(
          res => {
            res.unshift({ code: null, name: '全部', type: null });
            this.countryList = res;
          }
        )
        .catch(
          error => {
            const _msg = error.error.message || error.message || '获取失败';
            this.$message({ type: 'error', message: _msg });
          }
        );
    },
    getPageList(page) {
      this.selectedMsg.Page = page || 1;
      this.selectedMsg.SkipCount = (this.selectedMsg.Page - 1) * this.selectedMsg.MaxResultCount;
      this.selectedMsg.Type = 'Leader';
      this.selectedMsg.DistrictType = this.countryCode ? 'County' : this.cityCode ? 'City' : this.provinceCode ? 'Province' : null;
      this.selectedMsg.DistrictCode = this.countryCode || this.cityCode || this.provinceCode || null;
      this.selectedMsg.GenderCode = this.GenderCode;
      this.loading = true;
      api_GET_EventsManagementEventResultPlatformPointRankList(this.selectedMsg).then(res => {
        this.loading = false;
        this.tableData = res.items;
        this.total = res.totalCount;
      }, error => {
        this.loading = false;
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
      });
    },
    changeProvince(province) {
      this.cityCode = '';
      this.countryCode = '';
      this.cityList = [];
      this.countryList = [];
      if (province !== null) {
        this.cityCode = null;
        this.apiGetCitiesByProvince();
      }
      this.getPageList();
    },
    changeCity(city) {
      this.countryCode = '';
      this.countryList = [];
      if (city !== null) {
        this.countryCode = null;
        this.apiGetCountiesByCity();
      }
      this.getPageList();
    },
    changeCounty() {
      this.getPageList();
    },
    resetData() {
      this.selectedMsg.Keyword = null;
      this.provinceCode = null;
      this.cityCode = null;
      this.countryCode = null;
      this.cityList = [];
      this.countryList = [];
      this.GenderCode = null;
      this.getPageList();
    },
    clickEidt(row) {
      this.$refs.editIntegral.dialogVisible = true;
      this.$refs.editIntegral.selectedMsg.id = row.id;
      this.$set(this.$refs.editIntegral.selectedMsg, 'realPoints', row.realPoints);
    },
    // 数据导出
    reportData() {
      var that = this;
      that.buLoading = true;
      const tempUrl = `/api/excel`;
      const fileName = '领队积分排名' + '.xlsx';
      const params = {};
      params.ReportName = 'PlatformRank';
      params.Type = 'Leader';
      params.DistrictType = that.countryCode ? 'County' : that.cityCode ? 'City' : that.provinceCode ? 'Province' : '';
      params.DistrictCode = that.countryCode || that.cityCode || that.provinceCode || '';
      params.GenderCode = that.GenderCode || '';
      params.Keyword = that.selectedMsg.Keyword || '';
      api_GET_DownLoadFileAddHeader(tempUrl, fileName, params).then(res => {
        that.buLoading = false;
      }).catch(error => {
        that.buLoading = false;
        errorText(error).then(function(text) {
          that.$message({ type: 'error', message: text });
        });
      });
    },
    // 分页
    changePage(page) {
      this.selectedMsg.Page = page;
      this.getPageList();
    },
    changePageSize(pagesize) {
      this.selectedMsg.MaxResultCount = pagesize;
      this.getPageList();
    },
    // 获取静态图片
    getStaticFileUrl(relativeUrl) {
      if (relativeUrl) {
        return WEB_CONFIG.VUE_APP_STATIC_FILE_RUL + relativeUrl;
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.allNumber {
  font-size: 24px;
  font-weight: 700;
  text-align: center;
  color: #353434;
  width: 20px;
  height: 20px;
  line-height: 20px;
}
.numberOne {
  color: white;
  background: url('../../../../assets/images/integral_gold.png') no-repeat;
  background-size: 20px 20px;
}
.numberTwo {
  color: white;
  background: url('../../../../assets/images/integral_silver.png') no-repeat;
  background-size: 20px 20px;
}
.numberThree {
  color: white;
  background: url('../../../../assets/images/integral_bronze.png') no-repeat;
  background-size: 20px 20px;
}
.image_name {
  display: flex;
  align-items: center;
}
.table_photo {
  width: 60px;
  height: 60px;
  border-radius: 6px;
  margin-right: 10px;
}
.table_name {
  line-height: 60px;
}
</style>
